<?php include("siteTop.php"); ?>

<!--    
    Created By : Julian Ponce
    B.S. Computer Science - De La Salle Canlubang
    November 28, 2011
    Description :
    
    This page allows the admin to view the department-position details of a particular deptPosId.
-->
    
<?php 

//********************************************************************************************************
/*
 * Define the functions in here
 */
    
    // Start - Check if position has connection with any user
    function noAccountDeptPos($deptPosId) {
        $query = 
        "
            SELECT *
            FROM Account_Dept_Pos            
            WHERE deptPosId = '$deptPosId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if position has connection with any user 
    
    // Start - Check if $deptPosId has connection with RouteStepDeptPos
    function noRouteStepDeptPos($deptPosId) {
        $query = 
        "
            SELECT *
            FROM RouteStep_Dept_Pos            
            WHERE deptPosId = '$deptPosId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if $deptPosId has connection with RouteStepDeptPos
    
    // Start - Check if $deptPosId has connection with SentAttachment
    function noSentAttach($deptPosId) {
        $query = 
        "
            SELECT *
            FROM Sent_Attachment           
            WHERE receiverId = '$deptPosId' AND orgFlag = 'Internal'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if $deptPosId has connection with SentAttachment
    
//********************************************************************************************************
   
    // THIS SERVES AS THE TEMPORARY DEPT POS ID
    $deptPosId = $_POST['deptPosId'];
   
    //CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // IDENTIFIES DEPT-POS DETAILS
    $query = 
        "
            SELECT DP.deptPosId as deptPosId, DP.departmentId as departmentId, DP.positionId as positionId, D.departmentName as departmentName, P.positionName as positionName
            FROM Department_Position as DP, Department as D, Position as P
            WHERE DP.deptPosId = '$deptPosId' AND
                  DP.departmentId = D.departmentId AND
                  DP.positionId = P.positionId
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    
    $departmentId = $row['departmentId'];
    $positionId = $row['positionId'];
    $departmentName = $row['departmentName'];
    $positionName = $row['positionName'];
    
?>
<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>

    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery("#adminTools-nav").addClass("highlightMenuBar");
            jQuery("#orgSetup-nav").addClass("highlightMenuBar");
            jQuery("#posSetup-nav").addClass("highlightMenuBar");
            
            // [START] BACK BUTTON CLICKED > REDIRECT TO admDeptPosSetup.php
            jQuery("#backButton").click(function() {
                    window.location = 'admDeptPosSetup.php';                
            });
            
            // [START] GET SECURITY GROUPS CONNECTED TO THIS DEPARTMENT-POSITION
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/admGetSecGroups.php',
                dataType : 'json',
                data: {
                    deptPosId : "<?php echo $deptPosId; ?>"
                },
                success : function(data){
                   if (data) {
                        // PUT SECURITY GROUPS TO TABLE SECURITY GROUPS
                        jQuery.each(data, function(index, data) {
                            jQuery("#secGroupLastRow").before("<tr><td>"+data.securityGroupId+"</td><td>"+data.securityGroupName+"</td></tr>");
                        });
                   }                   
                },
                error : function() {
                    alert("fail in AJAX [admGetSecGroups.php]");
                }
            }); // [END] GET SECURITY GROUPS CONNECTED TO THIS DEPARTMENT-POSITION
            
            // [START] EDIT BUTTON CLICKED > submit deptPosEditForm and redirect user to admDeptPosEdit.php
            jQuery("#editButton").click(function() {
                tempDeptPosId = <?php echo $deptPosId; ?>;
                
                // SAVE PASSED DEPT POS ID
                jQuery("#passedDeptPosId").val(tempDeptPosId);

                // SUBMIT FORM FOR EDIT
                jQuery("#deptPosEditForm").submit();
            });
            
            // [START] BACK BUTTON CLICKED > REDIRECT TO admDeptPosSetup.php
            jQuery("#backButton").click(function() {
                    window.location = 'admDeptPosSetup.php';                
            });
            
            // Start - Delete the position
            jQuery('#deleteButton').live("click",function() {
                var contDelete = confirm("Do you really want to delete " + jQuery("#positionName").val() + " of " + jQuery("#departmentName").val() + " department?");
                
                if ( contDelete == true ) {
                    // Start - Ajax for deleting the position
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/admDeletePos.php',
                        dataType : 'json',
                        async : false,
                        data: {
                            deptPosId : jQuery("#deptPosId").val()
                        },
                        success : function(data){
                            // If, successful deletion of position
                            if (data) {
                                if ( data[0].flag == "success" ) {
                                    jQuery("#deptPosSuccessMessage").val(data[0].msg);
                                    jQuery("#deptPosStpMsgForm").submit();
                                }
                                else {
                                    alert(data[0].msg);
                                }
                            } // End if, successful deletion of position
                        },
                        error : function() {
                            alert("Error : Deleting the position was not successful.");
                        }
                    }); // End - Ajax for deleting the position
                } // End if for confirmation of position
            }); // End - Delete the position

            // Start - Edit the position
            jQuery('#deptPosEditForm').live("click",function() {
                jQuery("#editPosForm").submit();
            }); // End - Edit the position
            
        }); // END - (DOCUMENT READY FUNCTION)
    </script>
    <!-- HEAD END -->


<?php include("siteMiddle.php"); ?>
    
<!-- START OF BODY -->
<div id="content">
    <h1>Position View Page</h1>
    <form>
        <span class="screenlet" >Position Information</span>
        <table>
<!--            <tr>
                <td>Department Position Id</td>
                <td><?php echo $deptPosId; ?></td>
            </tr>-->
            <tr>
                <td>Department Name : </td>
                <td>
                    <input type="hidden" name="deptPosId" id="deptPosId" value="<?php echo $deptPosId; ?>"/>
                    <input type="hidden" name="departmentId" id="departmentId" value="<?php echo $departmentId; ?>"/>
                    <input type="hidden" name="departmentName" id="departmentName" value="<?php echo $departmentName; ?>"/>
                    <?php echo $departmentName; ?>
                </td>
            </tr> 
            <tr>
                <td>Position Name : </td>
                <td>
                    <input type="hidden" name="positionId" id="positionId" value="<?php echo $positionId; ?>"/>
                    <input type="hidden" name="positionName" id="positionName" value="<?php echo $positionName; ?>"/>
                    <?php echo $positionName; ?>
                </td>
            </tr>
        </table>
        
        <!-- POSITION SECURITY GROUPS -->
        <span class="screenlet" >Security Groups</span>
        <table border="1">
            <tr>
                <th>Security Group Id</th>
                <th>Security Group Name</th>
            </tr>
            <tr id="secGroupLastRow" style="display:none;"/>
                <td> &nbsp </td>
                <td> &nbsp </td>
            </tr>
        </table>
        
        <!-- MESSAGE IF DEPT-POS CANNOT BE EDITED -->
        <span id="cannotEditMsg" style="display:none;">This dept-pos cannot be modified<br>because it has on-going transactions.<br><br></span>
        
        
    <!-- EDIT AND DELETE BUTTON -->
    <?php
    // Can be edited or deleted if no data in:
    // * No data in Account_Dept_Pos
    // * No data in RouteStep_Dept_Pos
    // * No data in Sent_Attachment
    if ( noAccountDeptPos($deptPosId) == true && noRouteStepDeptPos($deptPosId) == true && noSentAttach($deptPosId) == true ) {
        ?>
        <input type="Button" name="editButton" id="editButton" class="siteGreenButton" value="Edit"/>
        <input type="Button" name="deleteButton" id="deleteButton" class="siteRedButton" value="Delete"/>
        <?php
    }
    ?>
    
    <!-- BACK BUTTON -->
    <input type="Button" name="backButton" id="backButton" class="siteGreenButton" value="Back"/>
    </form>
    
    <!-- THIS WILL SERVE AS THE FORM IN ORDER TO PASS deptPosId TO admEditPos.php -->    
    <form id="deptPosEditForm" action="admEditPos.php" method="POST">
        <input type="hidden" name="deptPosId" id="passedDeptPosId"value="<?php echo $deptPosId; ?>"/>
    </form>
    
    <!-- Hidden form to be used when the delete of position was successful -->
    <form id="deptPosStpMsgForm" name="deptPosStpMsgForm" action="admDeptPosSetup.php" method="POST">
        <input type="hidden" name="deptPosSuccessMessage" id="deptPosSuccessMessage" value=""/>
    </form>

</div> 

<!-- BODY END -->
<?php 
    include ("siteBottom.php"); 
?>